class TasksetsController < ApplicationController
  include TasksetsHelper
  protect_from_forgery :except => :new 
  # GET /tasksets
  # GET /tasksets.xml
  def index
    @tasksets = Taskset.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @tasksets }
    end
  end

  # GET /tasksets/1
  # GET /tasksets/1.xml
  def show
    @taskset = Taskset.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @taskset }
    end
  end

  # GET /tasksets/new
  # GET /tasksets/new.xml
  def new
    if params[:name] != nil 
	 	@taskset = createSet(params[:name])
    	respond_to do |format|
      	format.html # new.html.erb
      	format.xml  { render :xml => @taskset }
    	end
    end
  end

  # GET /tasksets/1/edit
  def edit
    @taskset = Taskset.find(params[:id])
  end

  # POST /tasksets
  # POST /tasksets.xml
  def create
    @taskset = Taskset.new(params[:taskset])

    respond_to do |format|
      if @taskset.save
        format.html { redirect_to(@taskset) }
        format.xml  { render :xml => @taskset, :status => :created, :location => @taskset }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @taskset.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /tasksets/1
  # PUT /tasksets/1.xml
  def update
    @taskset = Taskset.find(params[:id])

    respond_to do |format|
      if @taskset.update_attributes(params[:taskset])
        format.html { redirect_to(@taskset) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @taskset.errors, :status => :unprocessable_entity }
      end
    end
  end

  def schedule
    @taskset = Taskset.find(params[:id])

    respond_to do |format|
      if @taskset.stage < 1 
			@taskset.update_attribute("stage", 1)
      end
		task_groups = @taskset.tasks.split(":");
      for task_group in task_groups
      	task_items = task_group.split(",");
         for task_item in task_items
         	@task = Task.find(task_item)
            if @task.stage < 1
            	@task.update_attribute("stage", 1);
				end
         end
      end
      format.html { redirect_to(tasksets_url) }
      format.xml  { head :ok }
    end
  end

  def stop
    @taskset = Taskset.find(params[:id])

    respond_to do |format|
		@taskset.update_attribute("stage", 4)
		task_groups = @taskset.tasks.split(":");
      for task_group in task_groups
      	task_items = task_group.split(",");
         for task_item in task_items
         	@task = Task.find(task_item)
            @task.update_attributes({"stage"=>5, "interrupt"=>9});
         end
      end
      format.html { redirect_to(tasksets_url) }
      format.xml  { head :ok }
    end
  end

  def reset
    @taskset = Taskset.find(params[:id])

    respond_to do |format|
      if @taskset.stage != 2 
			@taskset.update_attribute("stage", 0)
    	end
		task_groups = @taskset.tasks.split(":");
      for task_group in task_groups
      	task_items = task_group.split(",");
         for task_item in task_items
         	@task = Task.find(task_item)
            if @task.stage != 3 
            	@task.update_attribute("stage", 0);
            	@task.update_attribute("rid", 0);
         	end
      	end
      end
      format.html { redirect_to(tasksets_url) }
      format.xml  { head :ok }
    end
  end

  # DELETE /tasksets/1
  # DELETE /tasksets/1.xml
  def destroy
    @taskset = Taskset.find(params[:id])
    @taskset.destroy

    respond_to do |format|
      format.html { redirect_to(tasksets_url) }
      format.xml  { head :ok }
    end
  end

  def tasks
    @tasks = []
    @taskset = Taskset.find(params[:id])
    @task_groups = @taskset.tasks.split(":")
    for task_group in @task_groups
		@task_items = task_group.split(",")
        for task_item in @task_items
		  		@tasks << Task.find(task_item)
        end
    end

    respond_to do |format|
      format.html # tasks.html.erb
      format.xml  { render :xml => @tasks }
    end
  end

  def task_reset
     @task = Task.find(params[:id])

     respond_to do |format|
      if @task.update_attributes({"stage"=>0, "interrupt"=>0, "rid"=>0});
        format.html { redirect_to('/tasksets/tasks/'+@task.sid.to_s) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
      end 
     end 
  end 

  def task_schedule
     @task = Task.find(params[:id])

     respond_to do |format|
        (@task.stage == 0) and (@task.update_attributes({"stage"=>1, "interrupt"=>0, "rid"=>0}))
        format.html { redirect_to('/tasksets/tasks/'+@task.sid.to_s) }
        format.xml  { head :ok }
     end 
  end 

  def task_stop
     @task = Task.find(params[:id])

     respond_to do |format|
        @task.update_attributes({"stage"=>5, "interrupt"=>9});
        format.html { redirect_to('/tasksets/tasks/'+@task.sid.to_s) }
        format.xml  { head :ok }
     end 
  end 

end
